﻿using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CardDetailDisplayer : UIPanel
{
    public CardPair cardPair;
    private CardScriptable _cardData;
    public TextMeshProUGUI costText;
    public TextMeshProUGUI descText;
    public TextMeshProUGUI titleText;
    public Image artImage;

    public SlotMarker[] slots;
    private int preClickSlot = -1;

    private Dictionary<int, CardPair> quickSlot;

    public void InitialCardData(CardPair pairData, ref Dictionary<int, CardPair> quickSlot)
    {
        this.cardPair = pairData;
        this.quickSlot = quickSlot;
        
        _cardData = cardPair.cardData;

        FlushQuickSlot();
        FlushCardDisplay();
    }

    public void FlushQuickSlot()
    {
        foreach (var pair in quickSlot)
        {
            slots[pair.Key].SetMarkerStatus(true, false);
        }
    }
    
    public void FlushCardDisplay()
    {
        costText.SetText(_cardData.cost.ToString());
        descText.SetText(string.Format(_cardData.description, _cardData.value));
        titleText.SetText(_cardData.title);

        artImage.sprite = _cardData.image;

        if (cardPair.belongSlot > -1)
        {
            slots[cardPair.belongSlot].SetMarkerStatus(true, true);
        }
    }

    private void Update()
    {
        if (isActive && Input.GetMouseButtonUp(0))
        {
            // TODO 判断快捷栏
            foreach (RaycastResult result in OnClickGenerate())
            {
                if (result.gameObject.name.Contains("Slot"))
                {
                    Debug.Log("------------------------");
                    // 判断是否是当前的所属
                    var slotName = result.gameObject.name;
                    slotName = slotName.Substring(slotName.Length - 1, 1);
                    var markerBelongSlot = Int32.Parse(slotName) - 1;

                    Debug.Log($"当前的是 : {cardPair.belongSlot}, 当前点击的栏位是 : {markerBelongSlot}");
                    
                    // 已经知道被点击到了 开始判断是不是第二次点击
                    
                    if (preClickSlot == markerBelongSlot)
                    {
                        Debug.Log("<color=#ff0000>跟上次点击的是同个槽位</color>");
                        // 然后开始设置槽位
                        // 想要设置的槽位有没卡牌占着
                        // 有的话就把它取消掉

                        if (cardPair.belongSlot > -1)
                        {
                            Debug.Log($"<color=#0000ff>卡牌本身的所属槽位是{cardPair.belongSlot}</color>");
                            slots[cardPair.belongSlot].SetMarkerStatus(false, false);
                            var oldBelongCardPair = quickSlot[cardPair.belongSlot];
                            
                            quickSlot[cardPair.belongSlot] = null;
                            oldBelongCardPair.SetBelongSlot(-1);
                        }
                        
                        if (quickSlot.ContainsKey(markerBelongSlot))
                        {
                            var oldBelongCardPair = quickSlot[markerBelongSlot];

                            oldBelongCardPair?.SetBelongSlot(-1);
                            quickSlot[markerBelongSlot] = null;
                            slots[markerBelongSlot].SetMarkerStatus(false, false);
                        }
                        
                        // 设置卡牌到所属的槽位
                        cardPair.SetBelongSlot(markerBelongSlot);
                        quickSlot[markerBelongSlot] = cardPair;
                        slots[markerBelongSlot].SetMarkerStatus(true, true);
                        
                        FlushQuickSlot();
                    }
                    
                    Debug.Log($"上次点击的是槽位 : {preClickSlot}");
                    preClickSlot = markerBelongSlot;
                }
            }
            // TODO 判断按钮
        }

        if (isActive && Input.GetMouseButtonUp(1))
        {
            // 先调用一下刷新
            GameObject.FindObjectOfType<DeckDisplayer>().FlushAllCard();
            
            UiManager.CloseCurrentPanel();
        }
    }
}
